﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Membership.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Class representing a membership
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Xml.Linq;

namespace LighthouseNET
{
    using System;

    /// <summary>
    ///  Class representing a membership
    /// </summary>
    public class Membership
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Membership"/> class. 
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <param name="account">
        /// The account.
        /// </param>
        /// <param name="user">The user object</param>
        private Membership(int id, string account, User user)
        {
            Id = id;
            User = user;
            Account = account;
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="Membership"/> class from being created.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="account">The accout.</param>
        private Membership(int id, int userId, string account)
        {
            Id = id;
            UserId = userId;
            Account = account;
        }

        #region Properties
        /// <summary>
        /// Gets the id.
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// Gets the user id.
        /// </summary>
        public int UserId { get; private set; }

        /// <summary>
        /// Gets the account.
        /// </summary>
        public string Account { get; private set; }

        public User User { get; private set; }
        #endregion

        /// <summary>
        /// Parses the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>Parsed user</returns>
        /// <exception cref="ArgumentNullException">Element is null or empty.</exception>
        internal static Membership Parse(XElement element)
        {
            if (element == null || element.IsEmpty)
            {
                throw new ArgumentNullException("element");
            }

// ReSharper disable PossibleNullReferenceException
            var id = int.Parse(element.Element("id").Value);
            var userId = int.Parse(element.Element("user-id").Value);
            var account = element.Element("account") != null ? element.Element("account").Value : element.Element("project").Value;

            if (element.Element("user") != null)
            {
                var user = User.Parse(element.Element("user"));
                return new Membership(id, account, user);
            }

            return new Membership(id, userId, account);
// ReSharper restore PossibleNullReferenceException

            
        }
    }
}
